package com.thenmoret.admin.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.thenmoret.pojo.PageBean;
import com.thenmoret.pojo.Type;
import com.thenmoret.service.TypeService;
import com.thenmoret.util.JsonResponseUtiil;

@RequestMapping("/admin/articleType")
@Controller
public class ArticleTypeController {

	@Autowired
	private TypeService typeService;
	
	
	
	
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public String delete(Type type,HttpServletResponse response) throws Exception
	{
		Integer i = typeService.deleteArticleType(type.getTypeId());
		Map<String,Object> map = new LinkedHashMap<>();
		if(i!=null)
		{
			map.put("success", "ture");
			JsonResponseUtiil.ResponseJson(map, response);
		}
		else
		{
			map.put("success", "false");
			JsonResponseUtiil.ResponseJson(map, response);
		}
		return null;
	}
	
	
	/**
	 * 保存或修改文章类型
	 * @param type
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public String save(Type type,HttpServletResponse response) throws Exception
	{
		if(type.getTypeId() == null)
		{
			typeService.AddArticleType(type);	
			Map<String,Object> map = new LinkedHashMap<>();
			map.put("success", "ture");
			JsonResponseUtiil.ResponseJson(map, response);
		}
		else
		{
			typeService.updateArticleType(type);
			Map<String,Object> map = new LinkedHashMap<>();
			map.put("success", "ture");
			JsonResponseUtiil.ResponseJson(map, response);
		}
		return null;
	}
	
	/**
	 * 文章类型名的列表展示
	 * @param page
	 * @param rows
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public String list(@RequestParam(value="page",required = false) String page,
			@RequestParam(value="rows",required = false) String rows,HttpServletResponse response) throws Exception
	{
		response.setCharacterEncoding("UTF-8");
		PageBean<Type> pageBean = new PageBean<>();
		//传入的当前页的判断
		if(page == null || page.trim().length() == 0)
		{
			page = "1";
		}
		pageBean.setCurrPage(Integer.parseInt(page));//封装当前页
		
		//总的记录数
		Integer allRecord = typeService.findAll().size();
		pageBean.setAllPage(allRecord);
		
		Integer allPage = null;
		pageBean.setPageSize(Integer.parseInt(rows));
		if(pageBean.getAllPage() % pageBean.getPageSize() == 0)
		{
			allPage = pageBean.getAllPage() / pageBean.getPageSize();
		}
		else
		{
			allPage = pageBean.getAllPage() / pageBean.getPageSize()+1;
		}
		
		pageBean.setAllPageSize(allPage);
		
		Integer start = (pageBean.getCurrPage()-1) * pageBean.getPageSize();
		Integer end = pageBean.getCurrPage() * pageBean.getPageSize();
		
		List<Type> list = typeService.findByPage(start, end);
		
		pageBean.setList(list);
		Map<String,Object> map = new LinkedHashMap<>();
		/**
		 * 由于前台是easyui  他的分页会自动解析json数据  
		 * 分为total 和rows   
		 * total是整个记录
		 * rows是返回的数据源  他会和返回的json进行比对 把对应的字段输入进去
		 */
		map.put("total", pageBean.getAllPage());
		map.put("rows", pageBean.getList());
		JsonResponseUtiil.ResponseJson(map, response);
		return null;
	}
	
}
